<template>
  <div class="app-container">
    <keep-alive>
    </keep-alive>
    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading" :rules="rules">
      <el-form-item label="企业名称：" prop="name" required>
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="地址：" prop="address" required>
        <el-input v-model="form.address"></el-input>
      </el-form-item>
      <el-form-item label="公司电话：" prop="phone" required>
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="公司规模：" prop="scale" required :rules="scaleRules">
        <el-input v-model="form.scale"></el-input>
      </el-form-item>
      <el-form-item label="公司介绍：" prop="introduce" required>
        <el-input  type="textarea" v-model="form.introduce"></el-input>
      </el-form-item>
      <el-form-item label="公司官网地址：" prop="url" required>
        <el-input v-model="form.url"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import subjectApi from '@/api/company'
import { mapActions, mapGetters, mapState } from 'vuex'


export default {
  data () {
    return {
      form: {
        id: '',
        name: '',
        address: '',
        scale: '',
        introduce: '',
        url: '',

        phone: ''
        // level: 1,
        // levelName: ''
      },
      checked: false,

      formLoading: false,

      rules: {
        name: [
          { required: true, message: '请填写企业名称', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请填写公司地址', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请选填写公司介绍', trigger: 'change' }
        ],
        url: [
          { required: true, message: '请填写公司', trigger: 'change' }
        ],
        phone: [
          { required: true, message: '请填写公司电话', trigger: 'change' }
        ],

        scale: [
          { required: true, message: '请选择公司规模', trigger: 'change' }
        ]
      },
      scaleRules: [
        {
          required: true,
          message: '公司规模是必填项',
          trigger: 'blur'
        },
        {
          validator: this.validateScale,
          trigger: 'blur'
        }
      ]
    }
  },
  props: {
    // 选项
    selectOptions: {
      type: Array,
      default () {
        return []
      }
    },
    // 是否禁用
    disabled: {
      type: Boolean,
      default: false
    },
    // 已选中选项
    mulSelecteds: {
      type: Array,
      default () {
        return []
      }
    },
    mulSelectLoading: {
      type: Boolean,
      default: false
    },
    collapseTags: {
      type: Boolean,
      default: true
    }
  },
  created () {
    let id = this.$route.query.id

    let _this = this
    if (id) {
      _this.formLoading = true
      subjectApi.select(id).then(re => {
        _this.form = re.response
        _this.formLoading = false
      })
    }

  },
  watch: {
    mulSelecteds: {
      handler (newVal, oldVal) {
        this.selectedArr = newVal
        if (this.selectedArr.length &&
            this.selectedArr.length === this.selectOptions.length) {
          this.checked = true
        }
      },
      immediate: true
    }
  },
  selectAll () {
    this.selectedArr = []
    if (this.checked) {
      this.selectOptions.forEach((item) => { this.selectedArr.push(item.id) })
    } else {
      this.selectedArr = []
    }
    this.$emit('update:updateMultipleSelect', this.selectedArr)
  },
  changeSelect (val) {
    if (val.length === this.selectOptions.length) {
      this.checked = true
    } else {
      this.checked = false
    }
    this.$emit('update:updateMultipleSelect', this.selectedArr)
  },
  methods: {
    validateScale(rule, value, callback) {
      const isValid = /^[1-9]\d*$/.test(value); // Check if the value is a positive integer
      if (!isValid) {
        callback(new Error('请输入正整数'));
      } else {
        callback();
      }
    },
    submitForm () {
      let _this = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true
          subjectApi.edit(this.form).then(data => {
            if (data.code === 1) {
              _this.$message.success(data.message)
              _this.delCurrentView(_this).then(() => {
                _this.$router.push('/company/company/list')
              })
            } else {
              _this.$message.error(data.message)
              _this.formLoading = false
            }
          }).catch(e => {
            _this.formLoading = false
          })
        }
      })
    },
    resetForm () {
      let lastId = this.form.id
      this.$refs['form'].resetFields()
      this.form = {
        id: null,
        name: ''
        // level: 1,
        // levelName: ''
      }
      this.form.id = lastId
    },
    ...mapActions('tagsView', { delCurrentView: 'delCurrentView' })
  },
  computed: {
    ...mapGetters('enumItem', [
      'enumFormat'
    ]),
    ...mapState('enumItem', {
      levelEnum: state => state.user.levelEnum
    })
  }
}
</script>
